class xT_AICommand_Cmd_AttackEnemy extends xT_AICommand_Cmd;

//=============================================================================
// Events
//=============================================================================
function PostPopped()
{
	super.PostPopped();
	StopLatentExecution();
	if (Pawn != none)
	{
		Pawn.ZeroMovementVariables();
		Pawn.StopFire(0);
		EnemyLost();
	}
}

//=============================================================================
// State
//=============================================================================
AUTO state AttackEnemy
{
	function vector GetSightDistanceDifference()
	{
		local vector lshotpoint;
		lshotpoint = Pawn.Location + normal(xT_AIController(Outer).EnemyAim.Location - Pawn.Location) * ( abs(Vsize(xT_AIController(Outer).EnemyAim.Location - Pawn.Location) - Pawn.SightRadius + (Pawn.SightRadius * 0.1)));
		lshotpoint.Z = Pawn.Location.Z;
		return lshotpoint;
	}

	function vector GetEnemyDistanceDifference()
	{
		local vector lshotpoint;
		lshotpoint = Pawn.Location + normal(xT_AIController(Outer).EnemyAim.Location - Pawn.Location) * ( abs(Vsize(xT_AIController(Outer).EnemyAim.Location - Pawn.Location) - Pawn.Weapon.WeaponRange + (Pawn.Weapon.WeaponRange * 0.1)));
		lshotpoint.Z = Pawn.Location.Z;
		return lshotpoint;
	}

Begin:
	//`log("xT_AICommand_Attack executed");

Attacking:
	if ((Pawn != None) && (xT_AIController(Outer).EnemyAim != None) && (Pawn.Weapon != none) )
	{	
		// try to see enemy if beghind
		if (pawn.NeedToTurn(xT_AIController(Outer).EnemyAim.Location))
		{
			SetFocalPoint(xT_AIController(Outer).EnemyAim.Location);
			FinishRotation();
		}

		// HUMAN ORDER: try to see enemy in turned place. Note: Aliens not uses it
		if (Pawn.IsA('xT_Pawn_Human'))
		{
			if (!CanSee(xT_AIController(Outer).EnemyAim))
			{
				// if enemy is out of sight radius then go closer to enemy
				if (Vsize(Pawn.Location - xT_AIController(Outer).EnemyAim.Location) > Pawn.SightRadius)
				{
					xT_AIController(Outer).MoveToPosition(GetSightDistanceDifference(), false, true);
				}
			}
		}

		while(true && (xT_AIController(Outer).EnemyAim.Health > 0) && (CanSee(xT_AIController(Outer).EnemyAim)) && (!xT_AIController(Outer).EnemyAim.bFeigningDeath))
		{  
			//turn to enemy
			if (pawn.NeedToTurn(xT_AIController(Outer).EnemyAim.Location))
			{
				SetFocalPoint(xT_AIController(Outer).EnemyAim.Location);
				FinishRotation();
			}

			// fire to enemy
			if (pawn.CanAttack(xT_AIController(Outer).EnemyAim)) Pawn.StartFire(0);
			Sleep(WorldInfo.DeltaSeconds);

			// if distance > weapon range then chase target
			if ((xT_AIController(Outer).EnemyAim != None) && (pawn != none) && (Vsize(Pawn.Location - xT_AIController(Outer).EnemyAim.Location) > Pawn.Weapon.WeaponRange))
			{
				xT_AIController(Outer).MoveToPosition(GetEnemyDistanceDifference(), false, true);
			}
		}
		Pawn.StopFire(0);
		EnemyLost();
	}

End:
	PopCommand(Self);
}

//=============================================================================
// DefaultProperties
//=============================================================================
defaultproperties
{
}
